// components/address-cascader/index.js
/**
 * 该组件可以选择省市区或者是小区下面的XX栋XX单元XX层
 * 区别在于是否传villageId，如果传villageId表示的iu是XX栋XX单元XX层
 */ 
const $api = require("../../request/api").API
import {getNode} from "../../utils/util"
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      value: false,
      type: Boolean,
    },
    title:{
      value:"请选择所在地区",
      type:String
    },
    // 传过来的地址
    address:{
      value:{},
      type:Object
    },
    // 小区，如果有小区传过来就选择
    villageId:{
      value:"",
      type:Number||String
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    fieldNames:{
      text: 'name',
      value:"id",
    },
    options:[
    ],
  },
  observers:{
    async show(bool){
      if(!bool){
        return
      } 
      let {address,villageId} = this.data
      console.log(address,50)
      address.provinceId = ""
      address.provinceName = ""
      address.cityId = ""
      address.cityName = ""
      address.areaName = ""
      address.villageId = ""
      address.streetId = ""
      address.streetName = ""
      address.buildId = ""
      address.buildName = ""
      address.floorId = ""
      address.floorName = ""
      address.unitId = ""
      address.unitName = ""
      this.setData({
        address
      })
      console.log(this.data.addresss,"address")
      if(villageId){
        await this.getResidentAddressList(1)
        // 获取栋
      }else{
        await this.getAddressList(1)
      }
      console.log(this.data.options)
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    async handleChange({detail:{value,tabIndex,selectedOptions}}){
      const {address,villageId} = this.data
      let id = ['provinceId','cityId','areaId']
      let name = ['provinceName','cityName','areaName']
      if(villageId){
        id = ["buildId","unitId","floorId"]
        name = ["buildName","unitName","floorName"]
      }
      switch(selectedOptions.length){
        case 4:
          address.streetId = selectedOptions[3].id
          address.streetName = selectedOptions[3].name
        case 3:
          address[id[2]] = selectedOptions[2].id
          address[name[2]] = selectedOptions[2].name
        case 2:
          address[id[1]] = selectedOptions[1].id
          address[name[1]] = selectedOptions[1].name
        case 1:
          address[id[0]] = selectedOptions[0].id
          address[name[0]] = selectedOptions[0].name
      }
      this.setData({
        address,
      })
      villageId?await this.getResidentAddressList(tabIndex+2):await this.getAddressList(tabIndex+2)
    },
    // 获取地址列表
    async getAddressList(level=1){
      let {address,options} = this.data
      let {provinceId,cityId,areaId} = address
      let parentId = ""
      switch(level){
        case 2:
          parentId = provinceId
          break;
        case 3:
          parentId = cityId
          break;
        case 4:
          parentId = areaId
          break;
      }
      let res = await $api.getListLevelData({level,parentId})
      res = res.data.data||[]
      if(level==1){
        options = res
      }else if(res.length){
        const arr = getNode(options,parentId)
        arr.children = res
      }else if(!res.length){
        this.triggerEvent("confirm",JSON.parse(JSON.stringify(address)))
        this.setData({
          show:false
        })
      }
      console.log("地址",options)
      this.setData({
        options,
        address
      })
    },
    // 获取栋单元层列表
    async getResidentAddressList(level=1){
      let {address,options,villageId} = this.data
      let {buildId,unitId} = address
      let parentId = ""
      switch(level){
        case 2:
          parentId = buildId
          break;
        case 3:
          parentId = unitId
          break;
      }
      let res = await $api.getResidentAddressList({level:-level,parentId,villageId})
      // res = level!==-4?[]:(res.data.data||[])
      res = res.data.data||[]
      res.forEach(item=>delete item.children)
      if(level==1){
        options = res
      }else if(res.length){
        console.log(options)
        const arr = getNode(options,parentId)
        arr&&(arr.children = res)
      }else if(!res.length){
        this.triggerEvent("confirm",address)
        this.setData({
          show:false
        })
        return
      }
      this.setData({
        options,
        address
      })
    },
    // 取消
    cancel() {
      this.setData({
        show: false
      })
    },
    // 确定
    confirm() {
      const node = this.selectComponent("#van-picker")
      this.triggerEvent("confirm", { index: node.getIndexes()[0], value: node.getValues()[0] })
      this.cancel()
    },
  }

})
